<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sample extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Sample_model');
    }

    public function index() {
        if (!$this->checkLogin()) {
            header('Location: /biz/login/');
            return false;
        }
        $samples = $this->Sample_model->getAllSamples($this->bizId);
        $this->datas['samples'] = $samples;
        $this->load->view('sample/index', $this->datas);
    }

    /**
     * ajax
     */
    public function getSamplePics() {
        if (!$this->checkLogin()) {
            outputError('403', '请登录。');
            return false;
        }

        $sampleId = $this->input->post('id', true);
        $sampleId = intval($sampleId);
        if ($sampleId <= 0) {
            outputError('403', '非法ID。');
            return false;
        }

        $sample = $this->Sample_model->getSampleById($sampleId);
        if (!$sample) {
            outputError('403', '非法ID。');
            return false;
        }

        if ($this->bizId != $sample['busiId']) {
            outputError('403', '非法ID。');
            return false;
        }

        $pics = $this->Sample_model->getSamplePics($sampleId);
        $this->data['samplePics'] = $pics;
        outputJson('200', $this->data);
    }

    public function addSample() {
        if (!$this->checkLogin()) {
            header('Location: /biz/login/');
            return false;
        }

        $uploadPath = $this->config->config['sample_upload_path'];
        $uploadPath = $uploadPath.'cover/'.date('Y-m-d', time()).'/';
        $this->load->library('Cusupload');
        $this->cusupload->setAllowType('jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP');
        $this->cusupload->setAllowSize('2M');
        $this->cusupload->setEncriptname(true);
        $file = $this->cusupload->doUpload($uploadPath, 'sampleCover');
        if ($file === false) {
            outputError('400', '作品欣赏封面图上传失败。');
            return false;
        }
        $file = str_replace($this->config->config['sample_upload_path'], '', $file);
        $cover = $this->config->config['sample_url_path'].$file;

        $uploadPath = $this->config->config['sample_upload_path'];
        $uploadPath = $uploadPath.'pics/'.date('Y-m-d', time()).'/';
        $this->load->library('Cusupload');
        $this->cusupload->setAllowType('jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP');
        $this->cusupload->setAllowSize('2M');
        $this->cusupload->setEncriptname(true);
        $files = $this->cusupload->doUploadMulti($uploadPath, 'samplePics');
        if (!$files) {
            outputError('400', '作品图片上传失败。');
            return false;
        }

        foreach ($files as $key => $file) {
            $file = str_replace($this->config->config['sample_upload_path'], '', $file);
            $pic  = $this->config->config['sample_url_path'].$file;
            $files[$key] = $pic;
        }
        $files = array_values($files);

        $title  = $this->input->post('title', true);
        $intro  = $this->input->post('intro', true);
        $typeId = $this->input->post('typeId', true);
        $title  = textFilter($title);
        $intro  = textFilter($intro);
        $typeId = intval($typeId);
        if (!$title || !$intro || $typeId <= 0) {
            outputError('400', '请填写合法参数。');
            return false;
        }
        $typeInfo = $this->Sample_model->getTypeInfoById($this->bizId, $typeId);
        if (!$typeInfo) {
            outputError('400', '请填写合法参数。');
            return false;
        }
        $typeName = $typeInfo['name'];
        $addTime  = time();
        $this->Sample_model->addSample($this->bizId, $title, $intro, $typeId, $typeName, $addTime, $cover, $files);

        header('Location: /sample/');
        return true;
    }

    /**
     * ajax
     */
    public function delSample() {
        if (!$this->checkLogin()) {
            outputError('403', '请登录。');
            return false;
        }

        $sampleId = $this->input->post('id', true);
        $sampleId = intval($sampleId);
        if ($sampleId <= 0) {
            outputError('403', '非法ID。');
            return false;
        }

        $sample = $this->Sample_model->getSampleById($sampleId);
        if (!$sample) {
            outputError('403', '非法ID。');
            return false;
        }

        if ($this->bizId != $sample['busiId']) {
            outputError('403', '非法ID。');
            return false;
        }

        $this->Sample_model->delSample($sampleId);

        outputJson(200, 'OK');
    }

    /**
     * ajax
     */
    public function setTop() {
        if (!$this->checkLogin()) {
            outputError('403', '请登录。');
            return false;
        }

        $sampleId = $this->input->post('id', true);
        $sampleId = intval($sampleId);
        if ($sampleId <= 0) {
            outputError('403', '非法ID。');
            return false;
        }

        $sample = $this->Sample_model->getSampleById($sampleId);
        if (!$sample) {
            outputError('403', '非法ID。');
            return false;
        }

        if ($this->bizId != $sample['busiId']) {
            outputError('403', '非法ID。');
            return false;
        }

        $this->Sample_model->setTop($sampleId);

        outputJson(200, 'OK');
    }
}
